package com.ac.scum.log.web.model.player.controller;

import com.ac.common.core.exception.BaseException;
import com.ac.common.core.exception.config.ExceptionType;
import com.ac.common.core.result.Result;
import com.ac.scum.log.web.model.player.mapper.PlayerCustomizePackageChangeLogMapper;
import com.ac.scum.log.web.model.player.service.PlayerCustomizePackageService;
import com.ac.scum.log.web.utils.user.CurrentUserUtil;
import com.ac.scum.player.bean.PlayerConsumeTrade;
import com.ac.scum.player.bean.PlayerCustomizePackage;
import com.ac.scum.player.bean.PlayerCustomizePackageChangeLog;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.time.format.DateTimeFormatter;

/**
 * 自定义套餐
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("player_customize/package")
public class PlayerCustomizePackageController {

    private final PlayerCustomizePackageService playerCustomizePackageService;
    private final PlayerCustomizePackageChangeLogMapper playerCustomizePackageChangeLogMapper;


    /**
     * 玩家购买自定义套餐
     */
    @PostMapping("pay")
    public Result<Boolean> addPlayerCustomizePackage(@RequestBody PlayerConsumeTrade playerConsumeTrade) throws BaseException {
        return Result.ok(playerCustomizePackageService.pay(playerConsumeTrade));
    }

    /**
     * 玩家自定义套餐分页
     */
    @GetMapping("page/{current}/{size}")
    public Result<Object> playerCustomizePackagePage(@PathVariable("current") Long current, @PathVariable("size") Long size,
                                                     @RequestParam(defaultValue = "", required = false) String steamId,
                                                     @RequestParam(defaultValue = "", required = false) String packageName,
                                                     @RequestParam(defaultValue = "", required = false) String adminName,
                                                     @RequestParam(defaultValue = "", required = false) Boolean isActive) throws BaseException {
        return Result.ok(playerCustomizePackageService.page(new Page<>(current, size), CurrentUserUtil.getCurrentServerId(), steamId, adminName, packageName, isActive));
    }

    /**
     * 删除玩家自定义套餐
     */
    @DeleteMapping("{id}")
    public Result<Boolean> deletePlayerCustomizePackage(@PathVariable Long id) throws BaseException {
        return Result.ok(playerCustomizePackageService.removeById(id));
    }

    /**
     * 修改玩家自定义套餐
     */
    @PostMapping("update")
    public Result<Boolean> updatePlayerCustomizePackage(@RequestBody PlayerCustomizePackage playerCustomizePackage) throws BaseException {
        PlayerCustomizePackage byId = playerCustomizePackageService.getById(playerCustomizePackage.getId());
        StringBuilder log = new StringBuilder();
        if (!byId.getExpirationTime().equals(playerCustomizePackage.getExpirationTime()) && playerCustomizePackage.getExpirationTime() != null) {
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            log.append("过期时间变更: ").append(byId.getExpirationTime().format(formatter)).append(" -> ").append(playerCustomizePackage.getExpirationTime().format(formatter));
        }
        if (!byId.getSurplus().equals(playerCustomizePackage.getSurplus()) && playerCustomizePackage.getSurplus() != null) {

            if (playerCustomizePackage.getSurplus() < 0) {
                throw new BaseException(ExceptionType.Parameter_Error.of("剩余数量不可小于0"));
            }

            log.append("剩余数量变更: ").append(byId.getSurplus()).append(" -> ").append(playerCustomizePackage.getSurplus());
        }
        PlayerCustomizePackageChangeLog playerCustomizePackageChangeLog = new PlayerCustomizePackageChangeLog();
        playerCustomizePackageChangeLog.setPlayerCustomizePackageId(playerCustomizePackage.getId());
        playerCustomizePackageChangeLog.setLog(log.toString());
        playerCustomizePackageChangeLog.setServerId(CurrentUserUtil.getCurrentServerId());
        playerCustomizePackageChangeLogMapper.insert(playerCustomizePackageChangeLog);
        return Result.ok(playerCustomizePackageService.updateById(playerCustomizePackage));
    }

    @GetMapping("getChangeLog/by{id}")
    public Result<Object> getChangeLogByPlayerCustomizePackageId(@PathVariable Long id) throws BaseException {
        LambdaQueryWrapper<PlayerCustomizePackageChangeLog> wrapper = Wrappers.<PlayerCustomizePackageChangeLog>lambdaQuery().eq(PlayerCustomizePackageChangeLog::getServerId, CurrentUserUtil.getCurrentServerId())
                .eq(PlayerCustomizePackageChangeLog::getPlayerCustomizePackageId, id)
                .orderByDesc(PlayerCustomizePackageChangeLog::getCreateTime);
        return Result.ok(playerCustomizePackageChangeLogMapper.selectList(wrapper));
    }
}
